package com.bysc.userservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.commonutils.ResultCode;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.commonutils.constant.RedisConstant;
import com.bysc.commonutils.constant.UserConstant;
import com.bysc.commonutils.utils.JwtUtils;
import com.bysc.commonutils.utils.MD5;
import com.bysc.userservice.entity.UserInfo;
import com.bysc.userservice.entity.vo.LoginVo;
import com.bysc.userservice.entity.vo.RegisterVo;
import com.bysc.userservice.mapper.UserInfoMapper;
import com.bysc.userservice.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kkk
 * @since 2024-05-20
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @param loginVo 邮箱号 密码
     * @return
     */
    @Override
    public String login(LoginVo loginVo) {
        String email = loginVo.getEmail();
        String password = loginVo.getPassword();
        String code = loginVo.getCode();

        //获取会员
        UserInfo member = baseMapper.selectOne(new QueryWrapper<UserInfo>().eq("email", email));
        if (null == member) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.VALIDATEUSER_ERROR);
        }

        String rcode = (String) redisTemplate.opsForValue().get(RedisConstant.EMAIL + email);
        //校验密码和验证码
        if (!StringUtils.isEmpty(password)) {
            if (!MD5.encrypt(password).equals(member.getPassword())) {
                throw new BaiYunException(ResultCode.ERROR, MessageConstant.VALIDATEPWD_ERROR);
            }
        } else if (!StringUtils.isEmpty(code)) {
            if (!code.equals(rcode)) {
                throw new BaiYunException(ResultCode.ERROR, MessageConstant.VALIDATECODE_ERROR);
            }
        }else {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.INFO_ERROR);
        }
        //校验是否被禁用
        if (member.getIsDisabled()) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.DISABLEDUSER_ERROR);
        }
        //使用JWT生成token字符串
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname(),null);
        return token;
    }

    /**
     * @param registerVo 邮箱号、验证码、昵称、密码
     */
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册信息，进行校验
        String nickname = registerVo.getNickname();
        String email = registerVo.getEmail();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
//校验参数
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(email) || StringUtils.isEmpty(password) || StringUtils.isEmpty(code)) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.INFO_ERROR);
        }
//校验校验验证码
//从redis获取发送的验证码
        String mobleCode = (String) redisTemplate.opsForValue().get(RedisConstant.EMAIL + email);
        if (!code.equals(mobleCode)) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.VALIDATECODE_ERROR);
        }
//查询数据库中是否存在相同的邮箱号码
        Integer count = baseMapper.selectCount(new QueryWrapper<UserInfo>().eq("email", email));
        if (count.intValue() > 0) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.REGISTERED_USER_ERROR);
        }
//添加注册信息到数据库
        UserInfo member = new UserInfo();
        member.setNickname(nickname);
        member.setEmail(registerVo.getEmail());
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar(UserConstant.DEFAULT_AVATAR);
        this.save(member);
    }
}
